package com.lucenly.card.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.lucenly.card.R;
import com.luozm.captcha.Captcha;

public class CodeDialog extends Dialog{
    public CodeDialog(@NonNull Context context) {
        super(context, R.style.alert_dialog);
        setContentView(R.layout.dialog_code);
        final Captcha captcha = (Captcha) findViewById(R.id.captCha);
        initView(context);
        captcha.setCaptchaListener(new Captcha.CaptchaListener() {
            @Override
            public String onAccess(long time) {
                dismiss();
                return "验证通过,耗时"+time+"毫秒";
            }

            @Override
            public String onFailed(int failedCount) {
                captcha.reset(true);
                return "验证失败,已失败"+failedCount+"次";
            }

            @Override
            public String onMaxFailed() {
                captcha.reset(true);
                return "验证失败次数过多,请稍后再试";
            }
        });
    }

    private void initView(Context context) {
        LinearLayout lLayout_bg = (LinearLayout) findViewById(R.id.lLayout_bg);
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        setCanceledOnTouchOutside(false);
        // 调整dialog背景大小
        lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.8), LinearLayout.LayoutParams.WRAP_CONTENT));
        setCanceledOnTouchOutside(true);
    }
}
